## ---------------------------
##
## Script name: Nation_graphing_helpers.R
##
## Description: Functions that are used in National_Analysis.R
## that generate tract groups (by race/ethnicity, poverty, violence 
## quintile) for certain year(s) and automatically formats
## neighborhood rates and exposures rates into the desired 
## table format for the output figures.
##
## ---------------------------


.generate_tract_labels_Nation <- function(crime_df, crime_col, year_min, year_max, aggregate=F) {
  
  crime_df_filtered <- crime_df %>%
    filter(year >= year_min, year <= year_max)
  
  if (aggregate) {
    grouping_vars <- c("city", "tract_2010")
  } else {
    grouping_vars <- c("city", "tract_2010", "year")
  }
    
  rates <- .calc_crime_rate(df=crime_df_filtered, 
                            per=10000, crime_col=crime_col, population_col="total_population", 
                            grouping_vars=grouping_vars) %>% 
    arrange(crime_rate)

  rates <- rates %>%
    group_by_at(grouping_vars[!grepl("tract", grouping_vars)]) %>%
    mutate(t = row_number(),
           breaks = floor(max(t)/5))
  
  quintiles <- rates %>%
    mutate(quintile = case_when(t >= 1 & t <= breaks ~ "First",
                                    t > breaks & t <= 2*breaks ~ "Second",
                                    t > 2*breaks & t <= 3*breaks ~ "Third",
                                    t > 3*breaks & t <= 4*breaks ~ "Fourth",
                                    t > 4*breaks ~ "Fifth"),
           quintile = case_when(quintile=="First" ~ "first_quintile",
                                     quintile=="Second"|quintile=="Third"|quintile=="Fourth" ~ "middle_quintiles",
                                     quintile=="Fifth" ~ "fifth_quintile"),
           category = "quintile") %>%
    select(all_of(grouping_vars), quintile)
  
  tract_labels <- crime_df_filtered %>%
    group_by_at(grouping_vars) %>%
    summarize(poverty_bin = ifelse(sum(total_below_pov_level)/sum(total_population) >= 0.3, "poor", "nonpoor"),
           majority_race = case_when(sum(total_population_white)/sum(total_population) >= 0.5 ~ "majority_white",
                                     sum(total_population_black)/sum(total_population) >= 0.5 ~ "majority_black",
                                     sum(total_population_hisp)/sum(total_population) >= 0.5 ~ "majority_hisp",
                                     sum(total_population_asian+total_population_pacisl)/sum(total_population) >= 0.5 ~ "majority_aapi",
                                      TRUE ~ "all_other_nonmajority")) %>%
    left_join(quintiles, by=grouping_vars) %>%
    arrange(tract_2010)
  
  tract_labels <- tract_labels %>%
    select(all_of(grouping_vars), quintile, poverty_bin, majority_race) %>%
    distinct()
  
  return(tract_labels)
}


.generate_nbhd_rates_table_Nation <- function(rates_df, rate_col) {
  
  df_grouped <- rates_df %>%
    group_by(year) %>%
    mutate(overall_rate = mean(!!sym(rate_col), na.rm=T))
  
  cols_to_group <- c("quintile", "poverty_bin", "majority_race")
  
  for (col in cols_to_group) {
    
    df_grouped <- df_grouped %>%
      group_by_at(c("year", col)) %>%
      mutate(!!sym(paste0(rate_col,"_", col)) := mean(!!sym(rate_col), na.rm=T))
    
  }
  
  df_grouped <- df_grouped %>%
    dplyr::select(year, overall_rate, starts_with(paste0(rate_col, "_")), all_of(cols_to_group))
  
  quintile_row <- df_grouped %>%
    ungroup() %>%
    dplyr::select(year, overall_rate, quintile, paste0(rate_col,"_quintile")) %>%
    distinct() %>%
    spread(key=quintile, value=paste0(rate_col,"_quintile"))
  
  poverty_bin_row <- df_grouped %>%
    ungroup() %>%
    dplyr::select(year, overall_rate, poverty_bin, paste0(rate_col,"_poverty_bin")) %>%
    distinct() %>%
    filter(!is.na(poverty_bin)) %>%
    spread(key=poverty_bin, value=paste0(rate_col,"_poverty_bin"))
  
  majority_race_row <- df_grouped %>%
    ungroup() %>%
    dplyr::select(year, overall_rate, majority_race,  paste0(rate_col,"_majority_race")) %>%
    distinct() %>%
    filter(!is.na(majority_race)) %>%
    spread(key=majority_race, value=paste0(rate_col,"_majority_race"))
  
  groups <- quintile_row %>%
    inner_join(poverty_bin_row, by=c("year", "overall_rate")) %>%
    inner_join(majority_race_row, by=c("year", "overall_rate")) %>%
    gather(key="group", value=!!sym(rate_col), 2:ncol(.))
  
  groups <- groups %>%
    mutate(year = as.character(year),
           category = case_when(grepl("quintile", group) ~ "Quintile",
                                grepl("majority", group) ~ "Race",
                                grepl("poor", group) ~ "Poverty",
                                TRUE ~ "Overall"),
           category = factor(category, levels=c("Overall", "Quintile","Race", "Poverty")),
           group = factor(group, levels=c("overall_rate","first_quintile","middle_quintiles","fifth_quintile",
                                          "majority_white","majority_black", "majority_hisp", "majority_aapi",
                                          "all_other_nonmajority",
                                          "poor","nonpoor")))
  

  groups <- groups %>%
    mutate(!!sym(rate_col) := round(!!sym(rate_col), 2)) %>%
    select(year, category, group, !!sym(rate_col)) %>%
    arrange(category, group)
  
  return(groups)

}
  

.generate_exposure_rates_table_Nation <- function(rates_df, rate_col) {

  t <- rates_df %>%
    group_by(year) %>%
    summarize(shootr_nation = sum(!!sym(rate_col) * (total_population/sum(total_population, na.rm=T)), na.rm=T),
           shootr_poverty = sum(!!sym(rate_col) * (total_below_pov_level/sum(total_below_pov_level, na.rm=T)), na.rm=T),
           shootr_nonpoverty = sum(!!sym(rate_col) * ((total_population-total_below_pov_level)/sum((total_population-total_below_pov_level), na.rm=T)), na.rm=T),
           shootr_white_race = sum(!!sym(rate_col) * (total_population_white/sum(total_population_white, na.rm=T)), na.rm=T),
           shootr_black_race = sum(!!sym(rate_col) * (total_population_black/sum(total_population_black, na.rm=T)), na.rm=T),
           shootr_hisp_race = sum(!!sym(rate_col) * (total_population_hisp/sum(total_population_hisp, na.rm=T)), na.rm=T),
           shootr_aapi_race = sum(!!sym(rate_col) * ((total_population_asian+total_population_pacisl)/sum((total_population_asian+total_population_pacisl), na.rm=T)), na.rm=T),
           shootr_other_race = sum(!!sym(rate_col) * (total_population_otherrace/sum(total_population_otherrace, na.rm=T)), na.rm=T)) %>%
    dplyr::select(year, shootr_nation, shootr_poverty, shootr_nonpoverty, shootr_white_race,
                  shootr_black_race, shootr_hisp_race, shootr_aapi_race, shootr_other_race) %>%
    filter(year == 2014 | year == 2020)
  
  t2 <- t %>%
    gather(key="group", value="shootr", starts_with("shootr"))
  
  return(t2)

}


